package com.mine.start.frame;

import com.mine.client.view.ImageIcons;
import com.mine.common.core.components.MyButton;
import com.mine.common.core.constants.CommonParam;
import com.mine.common.core.constants.Icons;
import com.mine.common.core.constants.Images;
import com.mine.common.core.listener.LoginButtonListener;
import com.sun.awt.AWTUtilities;

import javax.swing.*;
import javax.swing.border.MatteBorder;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.RoundRectangle2D;

/**
 * 基础窗口
 *
 * @author WangYX
 * @version 1.0.0
 * @date 2023/11/28 9:45
 */
public class LoginFrame extends JFrame implements CommonParam {

    /**
     * 宽，高
     */
    private static final int DEFAULT_WIDTH = 500, DEFAULT_HEIGHT = 500;

    public LoginFrame() {
        //设置窗口宽高
        this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        //设置标题
        this.setTitle(FrameDef.CUSTOM_MADE);
        //窗口居中
        Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
        Dimension screenSize = defaultToolkit.getScreenSize();
        setLocation(screenSize.width / 2 - DEFAULT_WIDTH / 2, screenSize.height / 2 - DEFAULT_HEIGHT / 2);
        //图标
        this.setIconImage(ImageIcons.LOGO_0.getImage());
        //布局方式设置为null
        this.getContentPane().setLayout(null);
        //退出操作
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 去掉窗口的装饰
        this.setUndecorated(true);
        //采用指定的窗口装饰风格
        this.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
        //设置窗体圆角，最后两个参数分别为圆角的宽度、高度数值，一般这两个数值都是一样的
        AWTUtilities.setWindowShape(this, new RoundRectangle2D.Double(0.0D, 0.0D, DEFAULT_WIDTH, DEFAULT_HEIGHT, 20.0D, 20.0D));
        //设置背景颜色，记住一定要修改frame.getContentPane()的颜色，因为我们看到的都是这个的颜色而并不是frame的颜色
        this.getContentPane().setBackground(Color.WHITE);

        //插入顶部背景图片
        //创建具有分层的JLayeredPane
        JLayeredPane layeredPane = new JLayeredPane() {{
            this.setBounds(new Rectangle(0, -5, 500, 150));
        }};
        this.getContentPane().add(layeredPane);
        //顶部背景图片
        topBackground(layeredPane);
        //关闭按钮
        closeFunction(layeredPane);
        //最小化功能
        minFunction(layeredPane);
        //用户名
        JTextField userNameField = new JTextField();
        username(userNameField);
        //密码
        JPasswordField passwordField = new JPasswordField();
        password(passwordField);

        MyButton myButton = new MyButton("登录", 0);
        myButton.setBounds(100, 400, 290, 50);
        myButton.addActionListener(new LoginButtonListener(this, userNameField, passwordField));
        this.getContentPane().add(myButton);
    }

    /**
     * 默认底部边框, 更改底部边框
     */
    private final MatteBorder defaultBottomBorder = new MatteBorder(0, 0, 1, 0, new Color(192, 192, 192)),
            blueBottomBorder = new MatteBorder(0, 0, 1, 0, new Color(12, 192, 192)),
            yellowBottomBorder = new MatteBorder(0, 0, 1, 0, new Color(255, 200, 0));
    private final Font defaultFont_14 = new Font(FontDef.FONT_NAME, Font.PLAIN, 14);

    /**
     * 用户名
     */
    private void username(JTextField userNameField) {
        this.getContentPane().add(Icons.makerLabel(100, 220, Images.USERNAME_30_30));
        //添加圆角的用户名输入框
        userNameField.setBounds(150, 210, 235, 50);
        userNameField.setBorder(defaultBottomBorder);
        userNameField.setText("用户名");
        userNameField.setFont(defaultFont_14);
        //默认设置输入框中的文字颜色为灰色
        userNameField.setForeground(Color.GRAY);
        userNameField.setColumns(10);
        userNameField.addFocusListener(new FocusAdapter() {

            //获取光标事件
            @Override
            public void focusGained(FocusEvent e) {
                //获取焦点时，输入框中内容是“用户名”，那么去掉输入框中显示的内容；
                String text = userNameField.getText();
                if ("用户名".equals(text.trim()) || "用户名不能为空字符".equals(text)) {
                    userNameField.setText("");
                    userNameField.setForeground(Color.BLACK); //设置颜色为黑色
                }
                userNameField.setBorder(blueBottomBorder);
            }

            //失去光标事件
            @Override
            public void focusLost(FocusEvent e) {
                //失去焦点时，如果输入框中去掉空格后的字符串为空串则显示用户名
                String text = userNameField.getText();
                if ("".equals(text)) {
                    userNameField.setText("用户名");
                    userNameField.setFont(defaultFont_14);
                    userNameField.setForeground(Color.GRAY); //默认设置输入框中的文字颜色为灰色
                } else if ("".equals(text.trim())) {
                    userNameField.setText("用户名不能为空字符");
                    userNameField.setFont(defaultFont_14);
                    userNameField.setForeground(Color.RED);
                    userNameField.setBorder(yellowBottomBorder);
                    return;
                }
                userNameField.setBorder(defaultBottomBorder);
            }
        });
        this.getContentPane().add(userNameField);
    }

    /**
     * 密码
     */
    private void password(JPasswordField passwordField) {
        this.getContentPane().add(Icons.makerLabel(100, 290, Images.PWD_LOCK_30_30));
        passwordField.setBounds(150, 280, 200, 50);
        passwordField.setBorder(defaultBottomBorder);
        passwordField.setText("密码");
        passwordField.setFont(defaultFont_14);
        //默认设置输入框中的文字颜色为灰色
        passwordField.setForeground(Color.GRAY);
        //显示密码输入框中内容
        passwordField.setEchoChar((char) 0);
        // 监听
        passwordField.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                //获取焦点时，输入框中内容是“用户名”，那么去掉输入框中显示的内容；
                String text = new String(passwordField.getPassword());
                if ("密码".equals(text.trim()) || "密码不支持为空字符".equals(text)) {
                    passwordField.setText("");
                    passwordField.setEchoChar('*');
                    passwordField.setForeground(Color.BLACK);
                }
                passwordField.setBorder(blueBottomBorder);
            }

            @Override
            public void focusLost(FocusEvent e) {
                //失去焦点时，如果输入框中去掉空格后的字符串为空串则显示用户名
                String text = new String(passwordField.getPassword());
                if ("".equals(text)) {
                    passwordField.setText("密码");
                    passwordField.setFont(defaultFont_14);
                    passwordField.setEchoChar((char) 0); // 显示密码输入框中内容（"密码"）
                    passwordField.setForeground(Color.GRAY);
                } else if ("".equals(text.trim())) {
                    passwordField.setText("密码不支持为空字符");
                    passwordField.setFont(defaultFont_14);
                    passwordField.setEchoChar((char) 0);
                    passwordField.setForeground(Color.RED);
                    passwordField.setBorder(yellowBottomBorder);
                    return;
                }
                passwordField.setBorder(defaultBottomBorder);
            }
        });
        this.getContentPane().add(passwordField);
        this.getContentPane().add(new JLabel("插入密码显隐的图片") {
            final int width = 30, height = 30;

            {
                this.setBounds(360, 290, width, height);
                this.setIcon(Images.EYE_HIDDEN_30_30);
                final JLabel jLabel = this;
                this.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mousePressed(MouseEvent e) {
                        jLabel.setIcon(Images.EYE_SHOW_30_30);
                        passwordField.setFont(defaultFont_14);
                        passwordField.setEchoChar((char) 0);
                    }

                    @Override
                    public void mouseReleased(MouseEvent e) {
                        jLabel.setIcon(Images.EYE_HIDDEN_30_30);
                        String text = new String(passwordField.getPassword());
                        if ("".equals(text.trim()) || "密码".equals(text.trim()) || "密码不支持为空字符".equals(text)) {
                            return;
                        }
                        passwordField.setEchoChar('*'); //显示密码输入框中内容
                        passwordField.setForeground(Color.BLACK); //设置颜色为黑色
                    }
                });
            }
        });

    }

    /**
     * 顶部图片
     */
    private void topBackground(JLayeredPane layeredPane) {
        layeredPane.add(new JPanel() {{
            this.setBounds(0, -5, 500, 150);
            this.add(new JLabel() {{
                this.setIcon(Images.LOGIN_HOUR_500_150_JPG);
            }});
        }}, JLayeredPane.DEFAULT_LAYER);

    }

    /**
     * 关闭功能
     */
    public void closeFunction(JLayeredPane layeredPane) {
        layeredPane.add(new JPanel() {{
            this.setOpaque(false);
            this.setBounds(455, 6, 40, 40);
            this.add(new JLabel() {{
                this.setIcon(Images.CLOSE_A_30_30);
                this.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseEntered(MouseEvent e) {
                        setIcon(Images.CLOSE_B_30_30);
                    }

                    @Override
                    public void mouseExited(MouseEvent e) {
                        setIcon(Images.CLOSE_A_30_30);
                    }

                    @Override
                    public void mouseClicked(MouseEvent e) {
                        System.exit(0);
                    }
                });
            }});
        }}, JLayeredPane.MODAL_LAYER);
    }

    /**
     * 最小化功能
     */
    public void minFunction(JLayeredPane layeredPane) {
        layeredPane.add(new JPanel() {{
            this.setOpaque(false);
            this.setBounds(420, 6, 40, 40);
            this.add(new JLabel() {{
                this.setIcon(Images.MIN_WIN_A_30_30);
                this.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseEntered(MouseEvent e) {
                        setIcon(Images.MIN_WIN_B_30_30);
                    }

                    @Override
                    public void mouseExited(MouseEvent e) {
                        setIcon(Images.MIN_WIN_A_30_30);
                    }

                    @Override
                    public void mouseClicked(MouseEvent e) {
                        setExtendedState(JFrame.ICONIFIED);
                    }
                });
            }});
        }}, JLayeredPane.MODAL_LAYER);
    }


}
